package com.biocar.service.impl;

import com.biocar.bean.Medicine;
import com.biocar.mapper.MedicineMapper;
import com.biocar.response.PageResponse;
import com.biocar.service.MedicineService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

/**
 * @author DeSen Xu
 * @date 2021-11-22 22:16
 */
@Service
public class MedicineServiceImpl implements MedicineService {

    private MedicineMapper medicineMapper;

    @Autowired
    public void setMedicineMapper(MedicineMapper medicineMapper) {
        this.medicineMapper = medicineMapper;
    }



    @Override
    public PageResponse<Medicine> queryMedicine(int index, int max, int sortOrder) {
        Integer totalMedicineCount = medicineMapper.getTotalMedicineCount();
        if (isOutOfRange(index, totalMedicineCount)) {
            return new PageResponse<>(totalMedicineCount, max, index, Collections.emptyList());
        }
        List<Medicine> medicines = medicineMapper.getMedicines(sortOrder, index, max);
        return new PageResponse<>(totalMedicineCount, max, index, medicines);
    }

    /**
     * 检查索引是否超出范围
     * @param index 索引
     * @param total 总行数
     * @return 返回true表示超出范围
     */
    public boolean isOutOfRange(int index, int total) {
        return index > total;
    }


}
